package com.cbts.action.info;

import java.util.List;


import com.cbts.entity.Gift;
import com.cbts.entity.Rule;
import com.cbts.service.info.ExchangeManagementService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
 * @author 邱鸿辉
 *
 */
public class ExchangeManagementAction extends ActionSupport{

	

	private static final long serialVersionUID = 1L;
	private ExchangeManagementService exchangeManagementService;	//提供点数兑换规则管理相关的服务
	private Gift gift;												//兑换物品
	private Rule rule;												//兑换规则
	private List<Gift> giftList;									//兑换物品列表
	private String check;											//被用户勾选的兑换物品的giftId构成的字符串
	private String operate;											//用户进行操作：删除或者修改兑换物品
	private String message;
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public String getCheck() {
		return check;
	}
	public void setCheck(String check) {
		this.check = check;
	}
	public String getOperate() {
		return operate;
	}
	public void setOperate(String operate) {
		this.operate = operate;
	}
	public ExchangeManagementService getExchangeManagementService() {
		return exchangeManagementService;
	}
	public void setExchangeManagementService(
			ExchangeManagementService exchangeManagementService) {
		this.exchangeManagementService = exchangeManagementService;
	}
	public List<Gift> getGiftList() {
		return giftList;
	}
	public void setGiftList(List<Gift> giftList) {
		this.giftList = giftList;
	}
	public Gift getGift() {
		return gift;
	}
	public void setGift(Gift gift) {
		this.gift = gift;
	}
	public Rule getRule() {
		return rule;
	}
	public void setRule(Rule rule) {
		this.rule = rule;
	}
	//获取兑换规则
	public String loadRule()
	{
		try{
			rule = exchangeManagementService.getRule(new Integer(1));//只有一条兑换规则，id为1
			if(rule == null)
			{
				rule=new Rule(new Integer(1),"规则1",1.0);//刚开始时不存在兑换规则，在这里创建
				if(exchangeManagementService.addRule(rule))
					message = "点数兑换规则初始化成功";
			}
			if(message != null)
			{
				ActionContext.getContext().put("message", message);
				message = null;
			}
			return SUCCESS;
		}catch(Exception e){
			e.printStackTrace();
			return INPUT;
		}
	}
	public String updateRule()
	{
		try{
			rule.setRuleId(new Integer(1));
			rule.setRuleName("规则1");
			if(exchangeManagementService.updateRule(rule))
				message ="点数兑换规则修改成功";
			else 
			{
				message ="点数兑换规则修改失败";
				return ERROR;
			}
			return SUCCESS;
		}catch(Exception e){
			e.printStackTrace();
			message ="点数兑换规则修改失败";
			return INPUT;
		}
	}
	public String addGift()
	{
		try{
			if(exchangeManagementService.getGiftByGiftName(gift.getGiftName())!=null)
			{
				message ="物品名已存在";
				return ERROR;
			}
			exchangeManagementService.addGift(gift);
			message = "增加物品成功";
			return SUCCESS;
		}catch(Exception e){
			e.printStackTrace();
/*			message = "增加物品失败";*/
			return INPUT;
		}
			
		
	}
	public String deleteGift()
	{
		if(exchangeManagementService.deleteGift(gift))
			message= "物品删除成功";
		else 
		{
			message ="物品删除失败";
			return ERROR;
		}
		return SUCCESS;
	}
/*	public String loadGift()
	{
		gift = exchangeManagementService.getGift(1);
		return SUCCESS;
	}*/
	//获取现金这种兑换物品
	public String loadMoney()
	{
		try{
			gift = exchangeManagementService.getGift(1);//设计时把现金当成一种兑换物品gift（特殊的兑换物品），其id为1
			return SUCCESS;
		}catch(Exception e){
			e.printStackTrace();
			return INPUT;
		}
	}
	
	public String updateGift()
	{
		try{
		/*if(exchangeManagementService.getGiftByGiftName(gift.getGiftName())!= null)
		{
			message ="物品名已存在,修改失败";
			return ERROR;
		}*/
			
		if(exchangeManagementService.sameGiftNameExist(gift.getGiftId(), gift.getGiftName()))
		{
			message ="物品名已存在";
			return ERROR;
		}
		if(exchangeManagementService.updateGift(gift))
				message ="物品修改成功";
		else 
		{
			message ="物品修改失败";
			return ERROR;
		}
		return SUCCESS;
		}catch(Exception e){
			e.printStackTrace();
			return INPUT;
		}

	}
	//修改现金这种兑换物品的数量
	public String updateMoney()
	{
		try{
			Gift g = exchangeManagementService.getGift(1);//获取现金这种兑换物品
			g.setNumber(gift.getNumber());//修改现金的数量（现金这种兑换物品的价值为1元，修改现金的数量就是修改库存金额）
			exchangeManagementService.updateGift(g);//更新现金这种兑换物品
		return SUCCESS;
		}catch(Exception e){
			e.printStackTrace();
			return INPUT;
		}
	}
	//获取所有的兑换物品，用户页面显示
	public String getAllGift()
	{
		try{
			giftList = exchangeManagementService.getAllGift();//获取所有的兑换物品
			if(message != null)
			{
				ActionContext.getContext().put("message", message);
				message = null;
			}
			return SUCCESS;
		}catch(Exception e){
			e.printStackTrace();
			return INPUT;
		}
	}
	//修改或者删除兑换物品
public String updateOrDeleteGift(){
		try{
			Gift gf = null;
			List<Gift> selectedList = null;
			if( giftList != null)	
				 selectedList = exchangeManagementService.getSelectedGiftList(giftList,check,operate);//获取用户勾选的兑换物品
			if(selectedList != null)
			{
				for(int i=0;i<selectedList.size();i++){
					gf = selectedList.get(i);
					for(int j=i+1;j<selectedList.size();j++)
						if(gf.getGiftName().equals(selectedList.get(j).getGiftName()))
						{
							message = "礼物名不可相同";
							return ERROR;
						}
				}
				if ("删除".equals(operate)) {
					if(exchangeManagementService.deleteGiftList(selectedList))// 批量删除兑换物品
					{
						message = "物品删除成功";
					}
					else
					{
						message = "物品删除失败";
					}
					
				} else if ("修改".equals(operate)) {
					if(exchangeManagementService.updateGiftList(selectedList))// 批量更新兑换物品
						message = "物品修改成功";
					else
					{
						message = "物品修改失败";
						return ERROR;
					}
				
				} 
			}
			return SUCCESS;
		}catch(Exception e){
			e.printStackTrace();
			if ("删除".equals(operate)){
				message = "物品删除失败";
			}else
				message = "物品修改失败";
			return INPUT;
		}
	}
}
